//
//  URLFather.m
//  JinYong_iOS_Store
//
//  Created by 王厚一 on 2017/10/17.
//  Copyright © 2017年 why. All rights reserved.
//

#import "URLFather.h"
#import "AFTool.h"

@implementation URLFather

IMP_SINGLETON(URLFather)

#pragma mark --------------------------- 账号密码登录 ----------------------------------
- (void)loginWithMobile:(NSString *)mobile password:(NSString *)password success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Index",@"met":@"checkStore",@"typ":@"json",@"user_name":mobile,@"user_password":password}];
    
    [AFTool postWithURL:Base_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
    
}


#pragma mark --------------------------- 登录的验证码 -----------------------------
- (void)getLoginCodeWithMobile:(NSString *)mobile success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Login",@"met":@"storeCode",@"typ":@"json",@"mobile":mobile}];
    
    [AFTool postWithURL:Login_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];

}

#pragma mark --------------------------- 使用验证码登录 ----------------------------------
- (void)loginWithMobile:(NSString *)mobile code:(NSString *)code success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Index",@"met":@"checkStoreYzm",@"typ":@"json",@"user_name":mobile,@"user_yzm":code}];
    
    [AFTool postWithURL:Base_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
}

#pragma mark --------------------------- 忘记密码的验证码 ----------------------------------
- (void)getResetCodeWithMobile:(NSString *)mobile success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Login",@"met":@"findPasswdCode",@"typ":@"json",@"mobile":mobile}];
    
    [AFTool postWithURL:Login_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
    
}


#pragma mark --------------------------- 忘记密码 ----------------------------------------
- (void)resetPasswdWithMobile:(NSString *)mobile code:(NSString *)code passwd:(NSString *)passwd success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Login",@"met":@"resetPasswd",@"typ":@"json",@"from":@"erp",@"mobile":mobile,@"user_code":code,@"user_password":passwd,@"user_account":mobile}];
    
    [AFTool postWithURL:Login_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];

}

#pragma mark --------------------------- 首页数据 ----------------------------------------
- (void)loadMainDataWithSuccess:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Index",@"met":@"getStoreIndex",@"typ":@"json",@"u":kRequestUserId,@"k":kRequestKey}];
    
    [AFTool postWithURL:Base_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];

        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];

}

#pragma mark --------------------------- 退出登录 ----------------------------------------
- (void)logoutWithSuccess:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Login",@"met":@"logout",@"typ":@"json",@"u":kRequestUserId,@"k":kRequestKey}];
    
    [AFTool postWithURL:Login_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
}

#pragma mark --------------------------- 订单列表 ----------------------------------------
- (void)getOrderListWithType:(NSString *)orderType firstRow:(NSString *)firstRow listRows:(NSString *)listRows success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Seller_Trade_Order",@"met":orderType,@"typ":@"json",@"u":kRequestUserId,@"k":kRequestKey,@"jy":@"1",@"firstRow":firstRow,@"listRows":listRows}];
    
    [AFTool postWithURL:Base_URL params:dict success:^(NSDictionary *result) {

        [MBProgressHUD hideHUD];

        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
}

#pragma mark --------------------------- 确认送达 ----------------------------------------
- (void)orderArrivedWithOrderID:(NSString *)orderID success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Buyer_Order",@"met":@"confirmOrder",@"typ":@"json",@"u":kRequestUserId,@"k":kRequestKey,@"order_id":orderID}];
    
    [AFTool postWithURL:Base_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
}

#pragma mark --------------------------- 取消订单 ----------------------------------------
- (void)orderCanceledWithOrderID:(NSString *)orderID success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];
    
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Seller_Trade_Order",@"met":@"orderCancel",@"typ":@"json",@"u":kRequestUserId,@"k":kRequestKey,@"order_id":orderID}];
    
    [AFTool postWithURL:Base_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];
        
        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
}

#pragma mark --------------------------- 接单 -------------------------------------------
- (void)orderReceiveWithOrderID:(NSString *)orderID success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Seller_Trade_Order",@"met":@"send",@"typ":@"json",@"u":kRequestUserId,@"k":kRequestKey,@"order_id":orderID,@"jy":@"1"}];
    
    [AFTool postWithURL:Base_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];

        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
    
}

#pragma mark --------------------------- 拒单 -------------------------------------------
- (void)orderRefuseWithOrderID:(NSString *)orderID success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"Seller_Trade_Order",@"met":@"orderCancel",@"typ":@"json",@"u":kRequestUserId,@"k":kRequestKey,@"order_id":orderID}];
    
    [AFTool postWithURL:Base_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];

        if (success) {
            success(result);
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
        
    }];
    
}

#pragma mark --------------------------- 修改密码 ------------------------------------------
- (void)exchangePasswordWithOld:(NSString *)oldPasswd new:(NSString *)newPasswd success:(successBlock)success failure:(failureBlock)failure {
    
    [MBProgressHUD showMessage:@"加载中"];

    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
    [dict addEntriesFromDictionary:@{@"ctl":@"User",@"met":@"priPwdToNewPwd",@"typ":@"json",@"u":kRequestUserId,@"k":kRequestKey,@"pripassword":oldPasswd,@"newpassword":newPasswd}];
    
    [AFTool postWithURL:Login_URL params:dict success:^(NSDictionary *result) {
        
        [MBProgressHUD hideHUD];

        if (success) {
            success(result);
        }
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUD];

        if (failure) {
            failure(error);
        }
    }];

}

@end
